package com.miage.kangourou.client.calendar;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.bradrydzewski.gwt.calendar.client.Calendar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.miage.kangourou.client.GetListesService;
import com.miage.kangourou.client.GetListesServiceAsync;
import com.miage.kangourou.client.beans.Utilisateur;
import com.miage.kangourou.shared.Const;

@SuppressWarnings("deprecation")
public class KangourouPanel extends FlowPanel {

	public Utilisateur utilisateurEnCours = new Utilisateur();

	private final GetListesServiceAsync getListesService = GWT
			.create(GetListesService.class);

	private static Integer LEFT_PANEL_WIDTH = 180;
	private final HorizontalPanel panelTitre = new HorizontalPanel();
	private final VerticalPanel verticalPanel = new VerticalPanel();
	private final HorizontalPanel horizontalPanel1 = new HorizontalPanel();
	private final HorizontalPanel horizontalPanel2 = new HorizontalPanel();
	private final VerticalPanel verticalPanel2 = new VerticalPanel();

	private final Map<String, GenericWidget> mapWidget = new HashMap<String, GenericWidget>();

	private final RechercheMultiCritereWidget multiCritereWidget;
	private final NewEventWidget newEventWidget;
	private final CalendarWidget calendarWidget;
	private final MenuWidget menuWidget;
	private final DatePickerWidget datePickerWidget;

	public KangourouPanel() {
		multiCritereWidget = new RechercheMultiCritereWidget(this);
		newEventWidget = new NewEventWidget(this);
		calendarWidget = new CalendarWidget(this);
		menuWidget = new MenuWidget(this);
		datePickerWidget = new DatePickerWidget(this);

		Widget multiCritere = buildWidget(multiCritereWidget);
		Widget newEvent = buildWidget(newEventWidget);
		Widget menu = buildWidget(menuWidget);
		Widget datePicker = buildWidget(datePickerWidget);
		final Widget calendar = buildWidget(calendarWidget);

		DeferredCommand.addCommand(new Command() {
			@Override
			public void execute() {
				calendar.setHeight(Window.getClientHeight() - 85 + "px");
			}
		});

		multiCritereWidget.configureWidget();
		newEventWidget.configureWidget();
		calendarWidget.configureWidget();
		menuWidget.configureWidget();
		datePickerWidget.configureWidget();

		setSize("100%", "100%");

		Label titrePage = new Label("EDT MIAGE BORDEAUX");
		titrePage.addStyleName("titreEdt");
		panelTitre.setSize("100%", "100%");
		panelTitre.add(titrePage);

		horizontalPanel1.add(newEvent);
		horizontalPanel1.setCellWidth(newEvent, LEFT_PANEL_WIDTH + "px");
		horizontalPanel1.setCellHorizontalAlignment(newEvent,
				HasHorizontalAlignment.ALIGN_CENTER);
		horizontalPanel1.add(menu);

		menu.setWidth((RootPanel.get().getOffsetWidth() - LEFT_PANEL_WIDTH - 5)
				+ "px");

		verticalPanel.add(horizontalPanel1);

		verticalPanel2.add(datePicker);
		verticalPanel2.add(multiCritere);
		verticalPanel2.setWidth("100%");
		verticalPanel2.setSpacing(2);

		horizontalPanel2.setSize("100%", "100%");
		horizontalPanel2.add(verticalPanel2);
		horizontalPanel2.setCellWidth(verticalPanel2, LEFT_PANEL_WIDTH + "px");

		horizontalPanel2.add(calendar);
		verticalPanel.add(horizontalPanel2);

		add(panelTitre);
		add(verticalPanel);

		connectUsingCookie();
	}

	public void addWidget(String widgetName, GenericWidget genericPanel) {
		mapWidget.put(widgetName, genericPanel);
	}

	private Widget buildWidget(GenericWidget genericPanel) {
		Widget w = genericPanel.createWidget();
		addWidget(genericPanel.getUID(), genericPanel);

		return w;
	}

	public void clickChangeDateButton(int numOfDays) {

		CalendarWidget calendarWidget = null;
		Calendar calendar = null;
		calendarWidget = (CalendarWidget) getWidget("calendar");
		calendar = calendarWidget.getCalendar();

		if (numOfDays == -1) {
			calendar.setDate(new Date());
		} else {
			calendar.addDaysToDate(numOfDays);
		}

		// Sunday: 0 ; Monday: 1
		// - offset the week view so its starts on Monday,
		// - refresh the layout
		// - update the DatePicker
		calendar.addDaysToDate(-1 * calendar.getDate().getDay() + 1);
		calendar.doLayout();

		DatePickerWidget datePickerWidget = null;
		DatePicker datePicker = null;
		datePickerWidget = (DatePickerWidget) getWidget("datepicker");
		datePicker = datePickerWidget.getDatePicker();
		datePicker.setValue(calendar.getDate());

		calendarWidget.update();

		MenuWidget mw = null;
		mw = (MenuWidget) getWidget("menu");

		mw.getTitle().setText("Semaine " + Util.getWeek(calendar.getDate()));
	}

	/**
	 * Connect using the client cookie
	 */
	public void connectUsingCookie() {
		String connectionCookie = Cookies.getCookie(Const.COOKIE_NAME);

		if (connectionCookie != null) {
			final AsyncCallback<Utilisateur> util = new AsyncCallback<Utilisateur>() {

				public void onFailure(Throwable caught) {
				}

				public void onSuccess(Utilisateur result) {
					utilisateurEnCours = result;
					if (!result.isEmpty()) {
						refreshAfterLogin();
					}

				}
			};
			getListesService.loginUsingCookie(connectionCookie, util);
		}
	}

	public GenericWidget getWidget(String widgetName) {
		return mapWidget.get(widgetName);
	}

	public void refreshAfterLogin() {

		calendarWidget.configureWidget();
		menuWidget.refreshAfterLogin();
		newEventWidget.configureWidget();
		updateCookie();

	}

	public void refreshAfterLogout() {
		calendarWidget.configureWidget();
		menuWidget.refreshAfterLogout();
		newEventWidget.configureWidget();

	}

	public void removeCookie() {
		Cookies.removeCookie(Const.COOKIE_NAME);
	}

	public void updateCookie() {

		System.out.println("Cookie has been set"
				+ Cookies.getCookie(Const.COOKIE_NAME));

		Date now = new Date();
		long nowLong = now.getTime();

		// Set the cookie duration for a year
		nowLong = nowLong + (1000 * 60 * 60 * 24 * 7 * 365);
		now.setTime(nowLong);

		Cookies.setCookie(Const.COOKIE_NAME, utilisateurEnCours.getCookie(),
				now);

		System.out.println("Cookie has been set"
				+ Cookies.getCookie(Const.COOKIE_NAME));
	}

}
